import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { AppUser } from '#/api/platform/appuser';

import { COMMON_DICT, USER_DICT } from '#/api';
import { $t } from '#/locales';

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'nickname',
      label: $t('user.nickname'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(
  onStatusChange?: (
    newStatus: string,
    row: T,
  ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions<AppUser>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'nickname',
      title: $t('user.nickname'),
    },
    {
      cellRender: {
        name: 'CellAvatar',
      },
      field: 'avatar',
      title: $t('user.avatar'),
    },
    {
      field: 'phoneNumber',
      title: $t('user.phoneNumber'),
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          {
            label: $t('common.man'),
            value: USER_DICT.GENDER_MAN,
          },
          {
            label: $t('common.woman'),
            value: USER_DICT.GENDER_WOMAN,
          },
          {
            label: $t('common.unknown'),
            value: USER_DICT.GENDER_UNKNOWN,
          },
        ],
      },
      field: 'gender',
      title: $t('user.gender'),
    },
    {
      field: 'createAt',
      title: $t('common.createTime'),
    },
  ];
}
